<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\BehaviorCategory;
use app\common\model\BehaviorImage;
use app\common\model\BehaviorRecord;
use EasyWeChat\Foundation\Application;

/**
 * 首页
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * @ApiTitle    (点评头条)
     * @ApiAuthor   (Zhenzh)
     * @ApiDate     (2020、2、17)
     * @ApiSummary  (描述)
     * @ApiBody     (Body)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/index/headlines)
     * @ApiHeaders  (name="token", type="string", required=true, description="请求的 Token")
     * @ApiParams   (name="school_id", type="int", required=true, description="学校 id", sample="")
     * @ApiParams   (name="limit", type="int", required=false, description="条数 默认5", sample="")
     * @ApiReturnParams   (name="code", type="integer", required=true, description="状态码 1正常 0错误", sample="1")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回", sample="")
     * @ApiReturn ()
     */
    public function headlines()
    {
        $school_id = $this->request->post('school_id', 0);
        $limit = $this->request->post('limit', 5);

        $results = BehaviorRecord::alias('record')
            ->join('__BEHAVIOR_RECORD_DETAIL__ detail', 'detail.record_id = record.id')
            ->where('record.school_id', $school_id)
            ->field('record.record_username,record.school_id,record.class_id,record.dorm_id,record.bed_id,record.student_id,record.json_data,record.created_at,detail.id,detail.behavior_category_id,detail.behavior_category_type,detail.bed_num')
            ->order('detail.id', 'desc')
            ->limit($limit)
            ->select();

        $data = [];
        if ($results) {
            foreach ($results as $key => $result) {
                $data[$key]['record_username'] = $result['record_username'];
                $data[$key]['created_at'] = $result['created_at'];

                switch ($result['behavior_category_type']) {
                    case BehaviorCategory::CLASSES :
                        $data[$key]['note'] = $result['json_data']['grade_name'] . $result['json_data']['class_name'];
                        break;
                    case BehaviorCategory::DORM :
                        $data[$key]['note'] = $result['json_data']['garden_name'] . $result['json_data']['building_name'] . '号楼' . $result['json_data']['dorm_name'] . '寝室';
                        break;
                    case BehaviorCategory::DORM_STUDENT :
                        $data[$key]['note'] = $result['json_data']['garden_name'] . $result['json_data']['building_name'] . '号楼' . $result['json_data']['dorm_name'] . '寝室' . $result['bed_num'] . '号床';
                        break;
                    case BehaviorCategory::STUDENT :
                        $data[$key]['note'] = '学生' . $result['json_data']['student_name'];
                        break;
                }
            }
        }


        $this->success('', $data);
    }

    public function test()
    {
//        $list = [
//            '今天',
//            '昨天',
//            '本周',
//            '上周',
//            '近7天',
//            '本月',
//            '上月',
//            '近30天',
//            '本季度',
//            '上季度',
//            '本年',
//            '上年',
//            '近一个月',
//            '近三个月',
//            ];
//        $data = [];
//        for ($i = 1; $i <= 14; $i++) {
//            $data[$i] = [$list[$i - 1] => get_time($i, false)];
//        }
//
//        $this->success('', $data);
    }

//    /**
//     * @ApiTitle    (首页轮播图)
//     * @ApiAuthor   (Zhenzh)
//     * @ApiDateTime (2019/8/16 9:54)
//     * @ApiSummary  (描述)
//     * @ApiBody     (Body)
//     * @ApiMethod   (POST)
//     * @ApiRoute    (/api/index/slides)
//     * @ApiHeaders  (name="token", type="string", required=true, description="请求的Token")
//     * @ApiReturnParams   (name="code", type="integer", required=true, description="状态码 1正常 0错误", sample="1")
//     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
//     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回", sample="")
//     * @ApiReturn ()
//     */
//    public function slides()
//    {
//        $data = [
//            '/uploads/images/20110412100351306.jpg',
//            '/uploads/images/20110412100431955.jpg',
//            '/uploads/images/20110412100453447.jpg',
//            '/uploads/images/20110412100518269.jpg',
//            '/uploads/images/20110412100542518.jpg',
//            '/uploads/images/20111014161542266.jpg',
//        ];
//        $this->success('', $data);
//    }
//
//    /**
//     * @ApiTitle    (获取公众号永久素材)
//     * @ApiAuthor   (Zhenzh)
//     * @ApiDateTime (2019/8/7 15:47)
//     * @ApiSummary  (描述)
//     * @ApiBody     (Body)
//     * @ApiMethod   (POST)
//     * @ApiRoute    (/api/index/materials)
//     * @ApiHeaders  (name="token", type="string", required=true, description="请求的Token")
//     * @ApiParams   (name="type", type="string", required=false, description="素材的类型，图片（image）、视频（video）、语音 （voice）、图文（news）", sample="")
//     * @ApiParams   (name="offset", type="int", required=false, description="从全部素材的该偏移位置开始返回，可选，默认 0，0 表示从第一个素材 返回", sample="")
//     * @ApiParams   (name="count", type="int", required=false, description="返回素材的数量，可选，默认 20, 取值在 1 到 20 之间", sample="")
//     * @ApiReturnParams   (name="code", type="integer", required=true, description="状态码 1正常 0错误", sample="1")
//     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
//     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回", sample="")
//     * @ApiReturn ()
//     */
//    public function materials()
//    {
//        $cache_key = 'materials';
//        $data = cache($cache_key);
//
//        $debug = $this->request->request('debug', 0);
//        if ($debug && $data) {
//            dd($data);
//        }
//
//        if (!$data) {
//            $type = $this->request->post('type', 'news');
//            $offset = $this->request->post('offset', 0);
//            $count = $this->request->post('count', 5);
//
//            $app = new Application(get_addon_config('wechat'));
//            $material = $app->material;
//
//            $materials = $material->lists($type, $offset, $count);
//
//            $data =[];
//            $data['item_count'] = $materials['item_count'];
//            $data['total_count'] = $materials['total_count'];
//            if (isset($materials['item'])) {
//
//                foreach ($materials['item'] as $key => $material) {
//
//                    $item['media_id'] = $material['media_id'];
//                    if ($material['content']) {
//                        $item['content']['create_time'] = $material['content']['create_time'];
//                        $item['content']['update_time'] = $material['content']['update_time'];
//
//                        foreach ($material['content']['news_item'] as $k => $news) {
//
//                            $item['content']['news_item'][] = [
//                                'title' => $news['title'],
//                                'digest' => $news['digest'],
//                                'content_source_url' => $news['content_source_url'],
//                                'show_cover_pic' => $news['show_cover_pic'],
//                                'url' => $news['url'],
//                                'thumb_media_id' => $news['thumb_media_id'],
//                                'thumb_url' => $news['thumb_url'],
//                            ];
//
//                        }
//                    }
//
//
//                    $data['item'][] = $item;
//                }
//            }
//
//            cache($cache_key, $data, 60);
//        }
//
//        $this->success('', $data);
//    }
//
//    /**
//     * @ApiTitle    (获取公众号指定素材)
//     * @ApiAuthor   (Zhenzh)
//     * @ApiDateTime (2019/8/7 15:47)
//     * @ApiSummary  (描述)
//     * @ApiBody     (Body)
//     * @ApiMethod   (POST)
//     * @ApiRoute    (/api/index/material)
//     * @ApiHeaders  (name="token", type="string", required=true, description="请求的Token")
//     * @ApiParams   (name="media_id", type="string", required=true, description="media_id", sample="")
//     * @ApiParams   (name="thumb_media_id", type="string", required=false, description="thumb_media_id", sample="")
//     * @ApiReturnParams   (name="code", type="integer", required=true, description="状态码 1正常 0错误", sample="1")
//     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
//     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回", sample="")
//     * @ApiReturn ()
//     */
//    public function material()
//    {
//        $media_id = $this->request->request('media_id');
//        $thumb_media_id = $this->request->request('thumb_media_id');
//
//        if (!$thumb_media_id)
//            $this->error('fail');
//
//        $app = new Application(get_addon_config('wechat'));
//        $material = $app->material;
//
//        $material = $material->get($media_id);
//
//        $data = [];
//        if ($thumb_media_id) {
//            $data['create_time'] = $material['create_time'];
//            $data['update_time'] = $material['update_time'];
//            foreach ($material['news_item'] as $index => $item) {
//                if ($thumb_media_id == $item['thumb_media_id']) {
//                    $data['news_item'][] = $item;
//                    break;
//                }
//            }
//        } else {
//            $data = $material;
//        }
//
//        $this->success('', $data);
//
//    }

}
